using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presenters;
using Dominio;
using System.Collections.Generic;


public partial class BugDetails : AbstractView
{
    private BugDestailsPresenter presenter;
    private String statePage = "new";
    public BugDetails()
    {
        presenter = new BugDestailsPresenter();
    }                                       
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        this.LogedIn();
        statePage = Request.QueryString.Get("statePage");
        

        if (Request.QueryString.Get("BugId") != "null")
        {
            Bug bug = presenter.getBugById(int.Parse(Request.QueryString.Get("BugId")));
            Session["selectedBug"] = bug;
            bugdescripttxt.Visible = (statePage.Equals("new") || statePage.Equals("editable")) && bug.isEditable(Session["user"]);
           
            bugdescripttxt.Value = bug.Description;
            bugDescripttext.Text = bug.Description;

            bugIdtxt.Text = bug.Id.ToString();

            bugSotxt.Text = bug.So;
            bugSotext.Text = bug.So;

            Soversiontxt.Text = bug.SoVersion;
            soVersiontext.Text = bug.SoVersion;

            bugTypeDrop.DataSource = Enum.GetNames(typeof(BugTypeEnum));
            bugTypeDrop.DataBind();
            bugTypeDrop.SelectedValue = bug.Tipo;
            bugTypetext.Text = bug.Tipo;
            bugStatustext.Text = bug.Status;
            bugStatusdrop.DataSource = this.getPossibleStatus(bug.Status);
            bugStatusdrop.DataBind();
            releaseIdtext.Text = bug.Release.Id.ToString();
            testerIdtxt.Text = bug.Tester.Id.ToString();
            pricetxt.Text = "$" + bug.Price.ToString();

            foreach (Control ctrl in Master.FindControl("ContentPlaceHolder1").Controls)
            {
                if (ctrl is TextBox || ctrl is DropDownList || ctrl is Button)
                {
                    ctrl.Visible = (statePage.Equals("new") || statePage.Equals("editable")) && bug.isEditable(Session["user"]);
                }
                if (ctrl is Label && ctrl.ID.Contains("text"))
                {
                    ctrl.Visible = statePage.Equals("info") || (statePage.Equals("editable") && !bug.isEditable(Session["user"]));
                }

            }
            releaseIdtext.Visible = true;
            
        }
    }

    protected IList<String> getPossibleStatus(String actulStatus)
    {
        IList<String> statusList = new List<String>();
        if (actulStatus.Equals(BugStatusEnum.Pending.ToString()))
        {
            statusList.Add(BugStatusEnum.Pending.ToString());
            statusList.Add(BugStatusEnum.Approved.ToString());
            statusList.Add(BugStatusEnum.NotApproved.ToString());
        }
        else if (actulStatus.Equals(BugStatusEnum.NotApproved.ToString()))
        {
            statusList.Add(BugStatusEnum.NotApproved.ToString());
            statusList.Add(BugStatusEnum.Disputed.ToString());
        }
        else if (actulStatus.Equals(BugStatusEnum.Disputed.ToString()))
        {
            statusList.Add(BugStatusEnum.Disputed.ToString());
            statusList.Add(BugStatusEnum.DisputedApproved.ToString());
            statusList.Add(BugStatusEnum.DisputedNotApproved.ToString());
        }
        return statusList;
    }
    protected void Update_Click(object sender, EventArgs e)
    {
        presenter.UpdateBug(int.Parse(bugIdtxt.Text), bugdescripttxt.Value, bugSotxt.Text, Soversiontxt.Text, bugTypeDrop.Text,int.Parse(releaseIdtext.Text),bugStatusdrop.Text, int.Parse(testerIdtxt.Text));
    }

    protected void details_Click(object sender, EventArgs e)
    {
        Response.Redirect("BugDetails.aspx?BugId=" + ((Bug)Session["selectedBug"]).Id);
    }
    protected void messageBoard_Click(object sender, EventArgs e)
    {
        Response.Redirect("MessageBoard.aspx?BugId=" + ((Bug)Session["selectedBug"]).Id);
    }
}
